1 module hip.jni.android.looper;
2 
3 /*
4  * Copyright (C) 2010 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 /**
20  * @addtogroup Looper
21  * @{
22  */
23 
24 /**
25  * @file looper.h
26  */
27 
28 extern(C):
29 
30 /**
31  * ALooper
32  *
33  * A looper is the state tracking an event loop for a thread.
34  * Loopers do not define event structures or other such things; rather
35  * they are a lower-level facility to attach one or more discrete objects
36  * listening for an event.  An "event" here is simply data available on
37  * a file descriptor: each attached object has an associated file descriptor,
38  * and waiting for "events" means (internally) polling on all of these file
39  * descriptors until one or more of them have data available.
40  *
41  * A thread can have only one ALooper associated with it.
42  */
43 struct ALooper;
44 
45 /**
46  * Returns the looper associated with the calling thread, or NULL if
47  * there is not one.
48  */
49 ALooper* ALooper_forThread();
50 
51 /** Option for for ALooper_prepare(). */
52 enum {
53     /**
54      * This looper will accept calls to ALooper_addFd() that do not
55      * have a callback (that is provide NULL for the callback).  In
56      * this case the caller of ALooper_pollOnce() or ALooper_pollAll()
57      * MUST check the return from these functions to discover when
58      * data is available on such fds and process it.
59      */
60     ALOOPER_PREPARE_ALLOW_NON_CALLBACKS = 1<<0
61 }
62 
63 /**
64  * Prepares a looper associated with the calling thread, and returns it.
65  * If the thread already has a looper, it is returned.  Otherwise, a new
66  * one is created, associated with the thread, and returned.
67  *
68  * The opts may be ALOOPER_PREPARE_ALLOW_NON_CALLBACKS or 0.
69  */
70 ALooper* ALooper_prepare(int opts);
71 
72 /** Result from ALooper_pollOnce() and ALooper_pollAll(). */
73 enum {
74     /**
75      * The poll was awoken using wake() before the timeout expired
76      * and no callbacks were executed and no other file descriptors were ready.
77      */
78     ALOOPER_POLL_WAKE = -1,
79 
80     /**
81      * Result from ALooper_pollOnce() and ALooper_pollAll():
82      * One or more callbacks were executed.
83      */
84     ALOOPER_POLL_CALLBACK = -2,
85 
86     /**
87      * Result from ALooper_pollOnce() and ALooper_pollAll():
88      * The timeout expired.
89      */
90     ALOOPER_POLL_TIMEOUT = -3,
91 
92     /**
93      * Result from ALooper_pollOnce() and ALooper_pollAll():
94      * An error occurred.
95      */
96     ALOOPER_POLL_ERROR = -4,
97 }
98 
99 /**
100  * Acquire a reference on the given ALooper object.  This prevents the object
101  * from being deleted until the reference is removed.  This is only needed
102  * to safely hand an ALooper from one thread to another.
103  */
104 void ALooper_acquire(ALooper* looper);
105 
106 /**
107  * Remove a reference that was previously acquired with ALooper_acquire().
108  */
109 void ALooper_release(ALooper* looper);
110 
111 /**
112  * Flags for file descriptor events that a looper can monitor.
113  *
114  * These flag bits can be combined to monitor multiple events at once.
115  */
116 enum {
117     /**
118      * The file descriptor is available for read operations.
119      */
120     ALOOPER_EVENT_INPUT = 1 << 0,
121 
122     /**
123      * The file descriptor is available for write operations.
124      */
125     ALOOPER_EVENT_OUTPUT = 1 << 1,
126 
127     /**
128      * The file descriptor has encountered an error condition.
129      *
130      * The looper always sends notifications about errors; it is not necessary
131      * to specify this event flag in the requested event set.
132      */
133     ALOOPER_EVENT_ERROR = 1 << 2,
134 
135     /**
136      * The file descriptor was hung up.
137      * For example, indicates that the remote end of a pipe or socket was closed.
138      *
139      * The looper always sends notifications about hangups; it is not necessary
140      * to specify this event flag in the requested event set.
141      */
142     ALOOPER_EVENT_HANGUP = 1 << 3,
143 
144     /**
145      * The file descriptor is invalid.
146      * For example, the file descriptor was closed prematurely.
147      *
148      * The looper always sends notifications about invalid file descriptors; it is not necessary
149      * to specify this event flag in the requested event set.
150      */
151     ALOOPER_EVENT_INVALID = 1 << 4,
152 }
153 
154 /**
155  * For callback-based event loops, this is the prototype of the function
156  * that is called when a file descriptor event occurs.
157  * It is given the file descriptor it is associated with,
158  * a bitmask of the poll events that were triggered (typically ALOOPER_EVENT_INPUT),
159  * and the data pointer that was originally supplied.
160  *
161  * Implementations should return 1 to continue receiving callbacks, or 0
162  * to have this file descriptor and callback unregistered from the looper.
163  */
164 alias ALooper_callbackFunc = int function (int fd, int events, void* data);
165 
166 /**
167  * Waits for events to be available, with optional timeout in milliseconds.
168  * Invokes callbacks for all file descriptors on which an event occurred.
169  *
170  * If the timeout is zero, returns immediately without blocking.
171  * If the timeout is negative, waits indefinitely until an event appears.
172  *
173  * Returns ALOOPER_POLL_WAKE if the poll was awoken using wake() before
174  * the timeout expired and no callbacks were invoked and no other file
175  * descriptors were ready.
176  *
177  * Returns ALOOPER_POLL_CALLBACK if one or more callbacks were invoked.
178  *
179  * Returns ALOOPER_POLL_TIMEOUT if there was no data before the given
180  * timeout expired.
181  *
182  * Returns ALOOPER_POLL_ERROR if an error occurred.
183  *
184  * Returns a value >= 0 containing an identifier (the same identifier
185  * `ident` passed to ALooper_addFd()) if its file descriptor has data
186  * and it has no callback function (requiring the caller here to
187  * handle it).  In this (and only this) case outFd, outEvents and
188  * outData will contain the poll events and data associated with the
189  * fd, otherwise they will be set to NULL.
190  *
191  * This method does not return until it has finished invoking the appropriate callbacks
192  * for all file descriptors that were signalled.
193  */
194 int ALooper_pollOnce(int timeoutMillis, int* outFd, int* outEvents, void** outData);
195 
196 /**
197  * Like ALooper_pollOnce(), but performs all pending callbacks until all
198  * data has been consumed or a file descriptor is available with no callback.
199  * This function will never return ALOOPER_POLL_CALLBACK.
200  */
201 int ALooper_pollAll(int timeoutMillis, int* outFd, int* outEvents, void** outData);
202 
203 /**
204  * Wakes the poll asynchronously.
205  *
206  * This method can be called on any thread.
207  * This method returns immediately.
208  */
209 void ALooper_wake(ALooper* looper);
210 
211 /**
212  * Adds a new file descriptor to be polled by the looper.
213  * If the same file descriptor was previously added, it is replaced.
214  *
215  * "fd" is the file descriptor to be added.
216  * "ident" is an identifier for this event, which is returned from ALooper_pollOnce().
217  * The identifier must be >= 0, or ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
218  * "events" are the poll events to wake up on.  Typically this is ALOOPER_EVENT_INPUT.
219  * "callback" is the function to call when there is an event on the file descriptor.
220  * "data" is a private data pointer to supply to the callback.
221  *
222  * There are two main uses of this function:
223  *
224  * (1) If "callback" is non-NULL, then this function will be called when there is
225  * data on the file descriptor.  It should execute any events it has pending,
226  * appropriately reading from the file descriptor.  The 'ident' is ignored in this case.
227  *
228  * (2) If "callback" is NULL, the 'ident' will be returned by ALooper_pollOnce
229  * when its file descriptor has data available, requiring the caller to take
230  * care of processing it.
231  *
232  * Returns 1 if the file descriptor was added or -1 if an error occurred.
233  *
234  * This method can be called on any thread.
235  * This method may block briefly if it needs to wake the poll.
236  */
237 int ALooper_addFd(ALooper* looper, int fd, int ident, int events,
238         ALooper_callbackFunc callback, void* data);
239 
240 /**
241  * Removes a previously added file descriptor from the looper.
242  *
243  * When this method returns, it is safe to close the file descriptor since the looper
244  * will no longer have a reference to it.  However, it is possible for the callback to
245  * already be running or for it to run one last time if the file descriptor was already
246  * signalled.  Calling code is responsible for ensuring that this case is safely handled.
247  * For example, if the callback takes care of removing itself during its own execution either
248  * by returning 0 or by calling this method, then it can be guaranteed to not be invoked
249  * again at any later time unless registered anew.
250  *
251  * Returns 1 if the file descriptor was removed, 0 if none was previously registered
252  * or -1 if an error occurred.
253  *
254  * This method can be called on any thread.
255  * This method may block briefly if it needs to wake the poll.
256  */
257 int ALooper_removeFd(ALooper* looper, int fd);